package mortal.game.control
{
    import Framework.MQ.*;
    import Message.Command.*;
    import Message.Game.*;
    import modules.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.view.player.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class PlayerSavvyController extends Controller
    {
        private var _savvyModule:PlayerSavvyModule;

        public function PlayerSavvyController()
        {
            
        }

        override protected function initView() : IView
        {
            if (this._savvyModule == null)
            {
                this._savvyModule = new PlayerSavvyModule();
            }
            return this._savvyModule;
        }

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.AcupointOpenSavvyModule, this.onSavvyModuleOpenHandler);
            Dispatcher.addEventListener(EventName.AcupointGetSavvyLevelInfo, this.onGetSavvyLevelInfoHandler);
            Dispatcher.addEventListener(EventName.AcupointGetSavvyLevelSuccess, this.onGetSavvyLevelSuccessHandler);
            Dispatcher.addEventListener(EventName.AcupointUpgradeSavvy, this.onUpgradeSavvyHandler);
            Dispatcher.addEventListener(EventName.AcupointUpgradeSavvySuccess, this.onUpgradeSavvySuccessHandler);
            Dispatcher.addEventListener(EventName.AcupointClearCdTime, this.onClearCdTimeHandler);
            Dispatcher.addEventListener(EventName.AcupointClearCdTimeSuccess, this.onClearCdTimeSuccessHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateSavvyRate, this.onSavvyRateChangeHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateAcupointInfo, this.onAcupointInfoReciveHandler);
            
        }

        private function onSavvyModuleOpenHandler(event:DataEvent) : void
        {
            var _loc_2:int = 0;
            if (view.isHide)
            {
                _loc_2 = event.data as int;
                this._savvyModule.setCurStep(_loc_2);
                GameProxy.acupoint.getSavvy(_loc_2);
                this._savvyModule.show();
            }
            else
            {
                view.hide();
            }
            
        }

        private function onGetSavvyLevelInfoHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            GameProxy.acupoint.getSavvy(_loc_2);
            
        }

        private function onGetSavvyLevelSuccessHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SGetSavvy;
            this._savvyModule.updateSavvyInfo(_loc_2);
            
        }

        private function onUpgradeSavvyHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data;
            var _loc_3:* = _loc_2.step;
            var _loc_4:* = _loc_2.addRate;
            GameProxy.acupoint.upgradeSavvy(_loc_3, _loc_4);
            
        }

        private function onUpgradeSavvySuccessHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data;
            var _loc_3:* = _loc_2.step as int;
            var _loc_4:* = _loc_2.leftCdTime as int;
            var _loc_5:* = _loc_2.newLevel as int;
            var _loc_6:* = _loc_2.result as Boolean;
            AcupointData.setSavvyLevel(_loc_3, _loc_5);
            this._savvyModule.upgradeReturn(_loc_4);
            if (_loc_6)
            {
                this._savvyModule.updateConfigInfo(_loc_3);
                MsgManager.addBroadCast("提升悟性成功");
            }
            else
            {
                MsgManager.addBroadCast("提升悟性失败");
            }
            
        }

        private function onClearCdTimeHandler(event:DataEvent) : void
        {
            GameProxy.acupoint.clearSavvyCd();
            
        }

        private function onClearCdTimeSuccessHandler(event:DataEvent) : void
        {
            this._savvyModule.upgradeReturn(0);
            
        }

        private function onSavvyRateChangeHandler(param1:MessageBlock) : void
        {
            var _loc_2:* = param1.messageBase as SSavvyRate;
            AcupointData.setSavvyRate(_loc_2);
            if (this._savvyModule == null)
            {
                
            }
            this._savvyModule.updateRate(_loc_2.rate, _loc_2.direction);
            
        }

        private function onAcupointInfoReciveHandler(param1:MessageBlock) : void
        {
            var _loc_3:int = 0;
            var _loc_2:* = param1.messageBase as SAcupointInfo;
            AcupointData.setInfo(_loc_2);
            if (this._savvyModule != null && !this._savvyModule.isHide)
            {
                _loc_3 = _loc_2.step;
                if (_loc_3 > 1 && _loc_3 <= 8)
                {
                    _loc_3 = _loc_3 - 1;
                }
                else if (_loc_3 == 1)
                {
                    _loc_3 = 8;
                }
                this._savvyModule.resetConfigInfo(_loc_3);
            }
            
        }

    }
}
